package com.ghost.alibabaghost.web.controller.sys;


import com.alibaba.fastjson.JSONObject;
import com.ghost.alibabaghost.api.service.sys.GhostUsersService;
import com.ghost.alibabaghost.api.model.sys.GhostUsersModel;
import com.ghost.alibabaghost.result.QueryPageInfo;
import com.ghost.alibabaghost.result.Code;
import com.ghost.alibabaghost.result.SingleResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 用户信息表(GhostUsers)表控制层
 *
 * @author yaolisheng
 * @since 2022-01-08 02:33:46
 */
@RestController
public class GhostUsersController {

    private static final Logger logger = LoggerFactory.getLogger(GhostUsersController.class);
    /**
     * 服务对象
     */
    @Autowired
    private GhostUsersService ghostUsersService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping("/selectOne")
    public SingleResult<GhostUsersModel> selectOne(@RequestParam("id") Long id) {
        logger.info("通过主键查询单条数据->params:id=" + id);
        try {
            GhostUsersModel ghostUsersModel = this.ghostUsersService.queryById(id);
            return SingleResult.buildSuccess(ghostUsersModel);
        } catch (Exception e) {
            logger.error("通过主键查询单条数据->params:" + id + e);
            return SingleResult.buildFailure();
        }
    }

    /**
     * 获取用户列表
     *
     * @param queryPageInfo
     * @return
     */
    @RequestMapping(value = "/getUsers")
    public SingleResult<QueryPageInfo<GhostUsersModel>> getUsers(QueryPageInfo<GhostUsersModel> queryPageInfo) {
        logger.info("获取用户列表->params:" + queryPageInfo.toString());
        try {
            GhostUsersModel lhUsersModel = JSONObject.parseObject(queryPageInfo.getData(), GhostUsersModel.class);
            List<GhostUsersModel> lhUsersModels = ghostUsersService.queryAll(lhUsersModel);

            queryPageInfo.pageHelper(lhUsersModels);
            logger.info("获取用户列表->response:" + queryPageInfo);
            return SingleResult.buildSuccess(queryPageInfo);
        } catch (Exception e) {
            logger.error("获取用户列表->" , e);
            return SingleResult.buildFailure();
        }
    }

    /**
     * 改变用户状态
     *
     * @param id
     * @param status
     * @return
     */
    @RequestMapping("/changeStatus")
    public SingleResult<Object> changeStatus(@RequestParam("id") Integer id, @RequestParam("status") Boolean status) {
        logger.info("获取用户列表->params:+id=" + id + ";status=" + status);
        GhostUsersModel ghostUsersModel = new GhostUsersModel();
        ghostUsersModel.setId(id);
        ghostUsersModel.setStatus(status);
        Integer update = ghostUsersService.update(ghostUsersModel);
        if (update > 0) return SingleResult.buildSuccess(Code.SUCCESS, "操作成功");
        return SingleResult.buildFailure(Code.ERROR, "出错了");
    }

    /**
     * 根据主键删除用户
     *
     * @param id
     * @return
     */
    @RequestMapping("/deleteUser")
    public SingleResult<String> deleteUser(@RequestParam("id") Long id) {
        logger.info("根据主键删除用户->param:id=" + id);
        if (ghostUsersService.deleteById(id)) return SingleResult.buildSuccess("删除成功！");
        return SingleResult.buildFailure();
    }

    /**
     * 用户保存
     *
     * @param lhUsersModel
     * @return
     */
    @RequestMapping("/saveUser")
    public SingleResult<String> saveUser(@RequestBody GhostUsersModel lhUsersModel) {
        logger.info("用户保存-->params:" + lhUsersModel);
        try {
            ghostUsersService.insert(lhUsersModel);
            return SingleResult.buildSuccess("保存成功");
        } catch (Exception e) {
            logger.error("用户保存-->params:" + lhUsersModel);
            return SingleResult.buildFailure();
        }
    }

    /**
     * 用户更新
     *
     * @param ghostUsersModel
     * @return
     */
    @RequestMapping("/updateUser")
    public SingleResult<String> updateUser(@RequestBody GhostUsersModel ghostUsersModel) {
        logger.info("用户更新-->params:" + ghostUsersModel);
        try {
            ghostUsersService.update(ghostUsersModel);
            return SingleResult.buildSuccess("更新成功");
        } catch (Exception e) {
            logger.error("用户更新-->params:" + ghostUsersModel);
            return SingleResult.buildFailure();
        }
    }
}
